package dao

import (
	"common/logs"
	"context"
	"core/alg"
	"core/entity"
	"core/repository"
)

const (
	UidRedisKey string = "Uid"
	UidBegin    int64  = 10000
)

type AccountDao struct {
	repository *repository.DbManager
}

func NewAccountDao(m *repository.DbManager) *AccountDao {
	return &AccountDao{
		repository: m,
	}
}

func (ad *AccountDao) IncrUid() int64 {
	incr, err := ad.repository.Redis.Incr(UidRedisKey, UidBegin)
	if err != nil {
		logs.Error("Incr err: %v", err)
		return alg.SnowFlakeID()
	}
	return incr
}

// Save 向 Mongo 中插入数据
func (ad *AccountDao) Save(ctx context.Context, account *entity.Account) error {
	collection := ad.repository.Mongo.Db.Collection("account")
	_, err := collection.InsertOne(ctx, account)
	if err != nil {
		return err
	}
	return nil
}
